package com.loongtech.bi.action.bireport;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.manager.report.OverviewReportManager;
import com.loongtech.bi.manager.report.OverviewReportManager.*;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 运营报表-概况Action
 *
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("session")
@RequestMapping("/bi/overviewReport")
public class OverviewReportBiAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
    private String queryPlatform = "0";  // 前端传过来的查询条件：平台
    private String queryChannel = "0";  // 前端传过来的查询条件：渠道
    private String queryServer = "0";  // 前端传过来的查询条件：区服

    public final static String tab1 = "pay";  // 付费额
    public final static String tab4 = "payUser";  // 付费人数
    public final static String tab2 = "payRadio";  // 付费率&新增当日付费率
    public final static String tab3 = "payArpu";  // 付费ARPU&活跃ARPU

    private String dateRadio = "7";  // 切换的天数：7=近7天。15=近15天。30=近30天，0=自选时间
    // private int slideDay = 1;  // 自选时间时，选择的天数
    private Date dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - 7 * Time.DAY));  // 自选时间：开始时间
    private Date dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY));  // 自选时间：结束时间

    private ChartUIData chartUIdata1;  // 在线趋势图
    private ChartUIData chartUIdata2;  // 留存情况图
    private ChartUIData chartUIdata3;  // 活跃情况图
    private ChartUIData chartUIdata5;  // 付费情况图
    private ChartUIData chartUIdata4;  // 游戏时长图
    private List<OverviewData> datas;  // 前端要显示的实时汇总数据
    private List<YesterDayOverviewData> yesterdayDatas;  // 前端要显示的昨日数据
	private List<OnlineChartUIData> dataList1; // 在线趋势临时数据
	private List<AliveChartUIData> dataList2;  // 留存情况临时数据
	private List<ActiveChartUIData> dataList3;  // 活跃情况临时数据
	private List<PayChartUIData> dataList5;  // 收入情况临时数据
	private List<PlayTimeChartUIData> dataList4;  // 游戏时长临时数据

	private String tabType = "pay";  // 当前选中的tab

	@Autowired
	OverviewReportManager overviewReportManager;

    @RequestMapping(value = "search.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            Date startDay = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            Date endDay = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);

            chartUIdata1 = null;
            chartUIdata2 = null;
            chartUIdata3 = null;
            chartUIdata4 = null;
            chartUIdata5 = null;

            yesterdayDatas = overviewReportManager.getYesterdayOverviewData(queryPlatform, queryChannel, queryServer);  // 昨日汇总数据
            datas = overviewReportManager.getOverviewData(startDay, endDay);  // 汇总数据

            dataList1 = overviewReportManager.getOnlineChartUIData(startDay, endDay);
            if (dataList1 != null && dataList1.size() > 0) {
                chartUIdata1 = new ChartUIData();
                chartUIdata1.setJsonType(ChartType.SPLINE.type);
                chartUIdata1.setJsonStr(new ChartTool(dataList1, OnlineChartUIData.class, "").loadData(chartUIdata1.getJsonType()));
            }
            dataList2 = overviewReportManager.getAliveChartUIData(startDay, endDay);
            if (dataList2 != null && dataList2.size() > 0) {
                chartUIdata2 = new ChartUIData();
                chartUIdata2.setJsonType(ChartType.SPLINE.type);
                chartUIdata2.setJsonStr(new ChartTool(dataList2, AliveChartUIData.class, "").loadData(chartUIdata2.getJsonType()));
            }
            dataList3 = overviewReportManager.getActiveChartUIData(startDay, endDay);
            if (dataList3 != null && dataList3.size() > 0) {
                chartUIdata3 = new ChartUIData();
                chartUIdata3.setJsonType(ChartType.COLUMN.type);
                chartUIdata3.setJsonStr(new ChartTool(dataList3, ActiveChartUIData.class, "").loadData(ChartType.COLUMN.type));
            }
            dataList5 = overviewReportManager.getPayChartUIData(startDay, endDay);
            if (dataList5 != null && dataList5.size() > 0) {
                chartUIdata5 = new ChartUIData();
                chartUIdata5.setJsonType(ChartType.SPLINE.type);
                chartUIdata5.setJsonStr(new ChartTool(dataList5, PayChartUIData.class, tabType).loadData(chartUIdata5.getJsonType()));
            }
            dataList4 = overviewReportManager.getPlayTimeChartUIData(startDay, endDay);
            if (dataList4 != null && dataList4.size() > 0) {
                chartUIdata4 = new ChartUIData();
                chartUIdata4.setJsonType(ChartType.SPLINE.type);
                chartUIdata4.setJsonStr(new ChartTool(dataList4, PlayTimeChartUIData.class, "").loadData(chartUIdata4.getJsonType()));
            }

            result.put("res", "0");
            result.put("yesterdayDatas", yesterdayDatas);
            result.put("datas", datas);
            result.put("chartUIdata1", chartUIdata1);
            result.put("chartUIdata3", chartUIdata3);
            result.put("chartUIdata2", chartUIdata2);
            result.put("chartUIdata5", chartUIdata5);
            result.put("chartUIdata4", chartUIdata4);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
    }


    // 切换标签页
    @RequestMapping(value = "onTabChange.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void onTabChange(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, @RequestParam String tabType, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            Date startDay = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            Date endDay = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);

            chartUIdata5 = null;

            dataList5 = overviewReportManager.getPayChartUIData(startDay, endDay);
            if (dataList5 != null && dataList5.size() > 0) {
                chartUIdata5 = new ChartUIData();
                chartUIdata5.setJsonType(ChartType.SPLINE.type);
                chartUIdata5.setJsonStr(new ChartTool(dataList5, PayChartUIData.class, tabType).loadData(chartUIdata5.getJsonType()));
            }

            result.put("res", "0");
            result.put("chartUIdata5", chartUIdata5);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
    }

    public ChartUIData getChartUIdata1() {
        return chartUIdata1;
    }

    public void setChartUIdata1(ChartUIData chartUIdata1) {
        this.chartUIdata1 = chartUIdata1;
    }

    public ChartUIData getChartUIdata2() {
        return chartUIdata2;
    }

    public void setChartUIdata2(ChartUIData chartUIdata2) {
        this.chartUIdata2 = chartUIdata2;
    }

    public ChartUIData getChartUIdata3() {
        return chartUIdata3;
    }

    public void setChartUIdata3(ChartUIData chartUIdata3) {
        this.chartUIdata3 = chartUIdata3;
    }

    public ChartUIData getChartUIdata4() {
        return chartUIdata4;
    }

    public void setChartUIdata4(ChartUIData chartUIdata4) {
        this.chartUIdata4 = chartUIdata4;
    }

    @Override
    public void search() {

    }

    @Override
    protected String csvContent() {
        return null;
    }

    @Override
    protected String csvFileName() {
        return null;
    }

    public Date getDateStart() {
        return dateStart;
    }

    public void setDateStart(Date dateStart) {
        this.dateStart = dateStart;
    }

    public Date getDateEnd() {
        return dateEnd;
    }

    public void setDateEnd(Date dateEnd) {
        this.dateEnd = dateEnd;
    }

    public ChartUIData getChartUIdata5() {
        return chartUIdata5;
    }

    public void setChartUIdata5(ChartUIData chartUIdata5) {
        this.chartUIdata5 = chartUIdata5;
    }

    public List<OnlineChartUIData> getDataList1() {
        return dataList1;
    }

    public void setDataList1(List<OnlineChartUIData> dataList1) {
        this.dataList1 = dataList1;
    }

    public List<AliveChartUIData> getDataList2() {
        return dataList2;
    }

    public void setDataList2(List<AliveChartUIData> dataList2) {
        this.dataList2 = dataList2;
    }

    public List<ActiveChartUIData> getDataList3() {
        return dataList3;
    }

    public void setDataList3(List<ActiveChartUIData> dataList3) {
        this.dataList3 = dataList3;
    }

    public List<PayChartUIData> getDataList5() {
        return dataList5;
    }

    public void setDataList5(List<PayChartUIData> dataList5) {
        this.dataList5 = dataList5;
    }

    public List<PlayTimeChartUIData> getDataList4() {
        return dataList4;
    }

    public void setDataList4(List<PlayTimeChartUIData> dataList4) {
        this.dataList4 = dataList4;
    }

    public static String getTab1() {
        return tab1;
    }

    public static String getTab4() {
        return tab4;
    }

    public static String getTab2() {
        return tab2;
    }

    public static String getTab3() {
        return tab3;
    }

    public String getDateRadio() {
        return dateRadio;
    }

    public void setDateRadio(String dateRadio) {
        this.dateRadio = dateRadio;
    }

    public String getTabType() {
        return tabType;
    }

    public void setTabType(String tabType) {
        this.tabType = tabType;
    }

    public List<OverviewData> getDatas() {
        return datas;
    }

    public void setDatas(List<OverviewData> datas) {
        this.datas = datas;
    }

    public List<YesterDayOverviewData> getYesterdayDatas() {
        return yesterdayDatas;
    }

    public void setYesterdayDatas(List<YesterDayOverviewData> yesterdayDatas) {
        this.yesterdayDatas = yesterdayDatas;
    }

    public String getQueryPlatform() {
        return queryPlatform;
    }

    public void setQueryPlatform(String queryPlatform) {
        this.queryPlatform = queryPlatform;
    }

    public String getQueryChannel() {
        return queryChannel;
    }

    public void setQueryChannel(String queryChannel) {
        this.queryChannel = queryChannel;
    }

    public String getQueryServer() {
        return queryServer;
    }

    public void setQueryServer(String queryServer) {
        this.queryServer = queryServer;
    }
}
